package com.so206.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.so206.mapper.AlipayTradeConfigMapper;
import com.so206.mapper.AlipayTradeRecordMapper;
import com.so206.po.AlipayTradeConfig;
import com.so206.po.AlipayTradeRecord;
import com.so206.service.AlipayTradeServices;
import com.so206.utils.BeanCheck;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class AlipayTradeServicesImpl implements AlipayTradeServices {

    @Autowired
    private AlipayTradeConfigMapper alipayTradeConfigMapper;

    @Autowired
    private AlipayTradeRecordMapper alipayTradeRecordMapper;

    @Override
    public AlipayTradeConfig getconfig(Long id) {
        return alipayTradeConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update_config(AlipayTradeConfig config) {
        AlipayTradeConfig model = alipayTradeConfigMapper.selectByPrimaryKey(config.getId());
        if (model != null) {
            BeanCheck.copyPropertiesIgnoreNull(config, model);
            model.setOptTime(new Date());
            alipayTradeConfigMapper.updateByPrimaryKey(model);
        }
    }

    @Override
    public PageInfo<AlipayTradeRecord> find_record_by_page(Integer page, Integer rows, Integer status, String tr_name, String tr_account, String sid) {
        PageInfo<AlipayTradeRecord> bean;
        PageHelper.startPage(page, rows);
        if (StringUtils.isEmpty(tr_account)) {
            tr_account = null;
        }
        if (StringUtils.isEmpty(tr_name)) {
            tr_name = null;
        }
        if (StringUtils.isEmpty(sid)) {
            sid = null;
        }
        List<AlipayTradeRecord> list = alipayTradeRecordMapper.selectByStatusNameOutOrderId(status, tr_name, tr_account, sid);
        return new PageInfo<>(list);
    }

    @Override
    public AlipayTradeRecord find_record_by_id(Long id) {
        return alipayTradeRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public Long save_record(AlipayTradeRecord record) {
        record.setCreateTime(new Date());
        record.setOptTime(new Date());
        alipayTradeRecordMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update_record(AlipayTradeRecord record) {
        AlipayTradeRecord model = alipayTradeRecordMapper.selectByPrimaryKey(record.getId());
        if (model != null) {
            BeanCheck.copyPropertiesIgnoreNull(record, model);
            model.setOptTime(new Date());
            alipayTradeRecordMapper.updateByPrimaryKey(model);
        }
    }

    @Override
    public void delete_record_by_id(Long id) {
        alipayTradeRecordMapper.deleteByPrimaryKey(id);
    }


}
